package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc
    ) {
        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows, key, sortBy, desc));

    }
    @PostMapping
    public ResponseEntity<String> savaBrand(BrandDTO brandDTO, @RequestParam("cids")List<Long> ids){
        brandService.savaBrand(brandDTO,ids);
        return ResponseEntity.status(HttpStatus.CREATED).body(null);

    }
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long id){


            return ResponseEntity.ok(brandService.queryBrandByCategoryId(id));

    }
    /**
     * 根据id查询品牌
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id){
        BrandDTO brand = brandService.queryById(id);
        return ResponseEntity.ok(brand);
    }
    /**
     * 根据品牌的id集合拿到品牌集合
     */
    @GetMapping("/list")
  public   ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList){
        return ResponseEntity.ok(brandService.queryByIds(idList));

    }
}
